package com.huidu.bitrade.config;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.math.BigDecimal;

public class CustomerBigDecimalSerialize extends JsonSerializer<BigDecimal> {

        @Override
        public void serialize(BigDecimal value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
                if(null != value) {
                        //返回到前端的数据若为数字类型,前端接收有可能丢失精度,所以需要转成字符串
                        //gen.writeNumber(value.stripTrailingZeros());
                        gen.writeString(value.stripTrailingZeros().toPlainString());
                        //stripTrailingZeros().toPlainString()是去除小数点后的0
                 }else {
                        //若为null，则写null
                        gen.writeNull();
                 }
         }
}
